package com.meida.module.bus.provider.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.mybatis.interceptor.PageInterceptor;
import com.meida.common.mybatis.model.PageParams;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.module.bus.provider.service.BusOrderAnswerService;
import com.meida.module.pay.client.entity.PayBalance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * 后台用户余额列表
 */
@Component
public class UserBalancePageListHandler implements PageInterceptor<PayBalance> {

    @Autowired
    private BusOrderAnswerService busOrderAnswerService;

    @Override
    public void prepare(CriteriaQuery<PayBalance> cq, PageParams pageParams, EntityMap params) {
        cq.clear();
        cq.select(PayBalance.class, "balanceId", "bindAmount", "amount", "userId", "userType", "companyId", "createTime");
        cq.orderByDesc("createTime");
    }

    @Override
    public void complete(CriteriaQuery<PayBalance> cq, List<EntityMap> result, EntityMap extra) {
        result.forEach(entityMap -> {
            Long userId = entityMap.getLong("userId");
            BigDecimal haveSettleAmount = busOrderAnswerService.sumOrderHaveSettleAmount(userId);
            BigDecimal haveNoSettleAmount = busOrderAnswerService.sumOrderHaveNoSettleAmount(userId);
            entityMap.put("haveSettleAmount", haveSettleAmount);
            entityMap.put("haveNoSettleAmount", haveNoSettleAmount);
        });
    }

}
